/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.BusinessLayer.Alumno;
import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.dataLayer.AlumnoData;
import com.sistema.gestionuniversitaria.dataLayer.CarreraData;
import com.sistema.gestionuniversitaria.dataLayer.ExcepcionGlobal;
import com.sistema.gestionuniversitaria.dataLayer.NoDataException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Max
 */
public class ActualizarAlumno extends javax.swing.JInternalFrame {

    /**
     * Creates new form ActualizarAlumno
     */
    AlumnoData alData = new AlumnoData();
    CarreraData caData = new CarreraData();

    public ActualizarAlumno() {
        initComponents();
    }

    public void llenarCampos(Alumno alumno) throws ExcepcionGlobal, NoDataException {
        jTBxNombre.setText(alumno.getNombre());
        jTBxApellidos.setText(alumno.getApellidos());
        jTBxEmail.setText(alumno.getEmail());
        jTBxTelefono.setText(alumno.getTelefono());
        Date date;
        date = alumno.getFechaNacimiento();
        jTBxFecha.setText(date.toString());
        Carrera carrera = caData.getCarreraPorCodigo(alumno.getCarrera().getCodigo());
        String nombre=carrera.getNombre().toString();
        jTbxCarrera.setText(nombre);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTBxCedula = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jBtnBuscar = new javax.swing.JButton();
        jTBxNombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTBxApellidos = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTBxEmail = new javax.swing.JTextField();
        jTBxTelefono = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTBxFecha = new javax.swing.JTextField();
        jTbxCarrera = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jBtnCancelar = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Actualizar Alumno");

        jLabel1.setText("Complete los siguientes dtos:");

        jLabel2.setText("Cédula:");

        jLabel3.setText("Nombre:");

        jBtnBuscar.setText("Buscar");
        jBtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnBuscarActionPerformed(evt);
            }
        });

        jTBxNombre.setEnabled(false);

        jLabel4.setText("Apellidos:");

        jTBxApellidos.setEnabled(false);

        jLabel5.setText("E-mail:");

        jLabel6.setText("Teléfono:");

        jTBxEmail.setEnabled(false);

        jTBxTelefono.setEnabled(false);

        jLabel7.setText("Carrera:");

        jLabel8.setText("Fecha");

        jTBxFecha.setEnabled(false);
        jTBxFecha.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTBxFechaMouseClicked(evt);
            }
        });

        jTbxCarrera.setEnabled(false);

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jBtnCancelar.setText("Cancelar");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel9.setText("En el formato (DD-MM-YYYY)");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel1)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(0, 0, Short.MAX_VALUE)
                                            .addComponent(jBtnBuscar))
                                        .addComponent(jTBxNombre)))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTBxCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTBxApellidos))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(18, 18, 18)
                                    .addComponent(jTBxEmail))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel6)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTBxTelefono))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel8)
                                    .addGap(18, 18, 18)
                                    .addComponent(jTBxFecha))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel7)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTbxCarrera))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(jLabel9)))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTBxCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnBuscar)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTBxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTBxApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTBxEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTBxTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTBxFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTbxCarrera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jBtnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String cedula, nombre, apellidos, email, telefono, fecha = null;
        Date d = null;
        cedula = jTBxCedula.getText().toUpperCase();
        nombre = jTBxNombre.getText();
        apellidos = jTBxApellidos.getText();
        email = jTBxEmail.getText();
        telefono = jTBxTelefono.getText();
        fecha = jTBxFecha.getText();
        try {
            alData.actualizarAlumno(cedula, nombre, apellidos, telefono, email, fecha);
            JOptionPane.showMessageDialog(null, "Éxito en la ejecución");
            this.dispose();
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(ActualizarAlumno.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(ActualizarAlumno.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jBtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnBuscarActionPerformed
        Alumno alumno = new Alumno();
        try {
            alumno = alData.getAlumnoPOrCedula(jTBxCedula.getText().toUpperCase());
            llenarCampos(alumno);
            jTBxNombre.setEnabled(true);
            jTBxApellidos.setEnabled(true);
            jTBxEmail.setEnabled(true);
            jTBxTelefono.setEnabled(true);
            jTBxFecha.setEnabled(true); 
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(ActualizarAlumno.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(ActualizarAlumno.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtnBuscarActionPerformed

    private void jTBxFechaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTBxFechaMouseClicked
        jTBxFecha.setText(" ");
    }//GEN-LAST:event_jTBxFechaMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnBuscar;
    private javax.swing.JButton jBtnCancelar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTBxApellidos;
    private javax.swing.JTextField jTBxCedula;
    private javax.swing.JTextField jTBxEmail;
    private javax.swing.JTextField jTBxFecha;
    private javax.swing.JTextField jTBxNombre;
    private javax.swing.JTextField jTBxTelefono;
    private javax.swing.JTextField jTbxCarrera;
    // End of variables declaration//GEN-END:variables
}
